package Offer.offer;

import java.util.Arrays;

/**
 * Created by 雷文康 on 2018/1/6.
 */
public class Test44扑克牌的顺子 {
    public static void main(String[] args) {
        int[] array = {4, 0, 3, 5, 7};
        System.out.println(isContinuous2(array));
    }

    public static boolean isContinuous(int[] array) {
        if (array == null || array.length == 0) {
            return false;
        }
        Arrays.sort(array);
        int count0 = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == 0) {
                count0++;
            }
        }
        int gap = 0;
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i] == 0) {
                continue;
            }
            if (array[i + 1] == array[i]) {
                return false;
            }
            gap += array[i + 1] - array[i] - 1;
        }
        if (count0 >= gap) {
            return true;
        }
        return false;
    }

    public static boolean isContinuous2(int [] numbers) {
        if (numbers == null || numbers.length < 5) {
            return false;
        }
        //计数排序 并计算0的个数
        int[] array = new int[14];
        int zeroCount = 0;
        //标记最大值和最小值
        int min = -1;
        int max = 14;
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == 0) {
                zeroCount++;
            } else {
                array[numbers[i]]++;
                if (array[numbers[i]] > 1) {
                    return false;
                }
                if (numbers[i] < min) {
                    min = numbers[i];
                }
                if (numbers[i] > max) {
                    max = numbers[i];
                }
            }
        }
        //计算gap的个数
        int gap = 0;
        for (int i = 1; i < array.length; i++) {
            if (i <= min || i >= max) {
                continue;
            }
            if (array[i] == 0) {
                gap++;
            }
        }
        return zeroCount >= gap;

    }
}
